﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Standard;
using YSL.Application.Services.WorkOrder;
using YSL.Application.Services.定时任务;
using YSL.Application.Ysl_Dto.Input;
using YSL.Application.Ysl_Dto.Input.WorkOrder;
using YSL.Application.Ysl_Dto.Output;
using YSL.Application.Ysl_Dto.定时任务DTO;
using GetWorkOrderNumberListDto = YSL.Application.Ysl_Dto.Output.GetWorkOrderNumberListDto;

namespace YSL.Api.Host.Controllers
{
    /// <summary>
    /// 工单管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class WorkOrderController : ControllerBase
    {
        private readonly ILogger<WorkOrderController> _logger;
        private readonly IWorkOrderService _workOrderService;
        private readonly IHangfireService _hangfireService;

        public WorkOrderController(ILogger<WorkOrderController> logger, IWorkOrderService workOrderService, IHangfireService hangfireService)
        {
            _logger = logger;
            _workOrderService = workOrderService;
            _hangfireService = hangfireService;
        }
        /// <summary>
        /// 添加工单
        /// </summary>
        /// <param name="input">添加工单的输入参数</param>
        /// <returns>返回添加工单的结果</returns>
        [HttpPost("AddWorkOrder")]
        public ApiResult<int> AddWorkOrder(AddWorkOrderInput input)
        {
            try
            {
                _logger.LogInformation("添加工单");
                return _workOrderService.AddWorkOrder(input);

            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 获取工单列表
        /// </summary>
        /// <returns>返回工单列表的查询结果</returns>
        [HttpGet("GetWorkOrderList")]
        public ApiPaging<WorkOrderListOutput> GetWorkOrderList([FromQuery]GetWorkOrderListInput input)
        {
            
            try
            {
                _logger.LogInformation("工单列表");
                return _workOrderService.GetWorkOrderList(input);

            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }

        /// <summary>
        /// 删除工单
        /// </summary>
        /// <param name="input">删除工单的输入参数</param>
        /// <returns>返回删除工单的结果</returns>
        [HttpPut("DeleteWorkOrder")]
        public ApiResult<int> DeleteWorkOrder(DeleWorkOrderInput input)
        {
            try
            {
                _logger.LogInformation("删除工单");
                return _workOrderService.DeleteWorkOrder(input);

            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 更新工单
        /// </summary>
        /// <param name="input">更新工单的输入参数</param>
        /// <returns>返回更新工单的结果</returns>
        [HttpPut("UpdateWorkOrder")]
        public ApiResult<int> UpdateWorkOrder(UpWorkOrderInput input)
        {
            try
            {
                _logger.LogInformation("修改工单");
                return _workOrderService.UpdateWorkOrder(input);

            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);

                throw;
            }
        }
        /// <summary>
        /// 根据日期获取总数量
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetWorkOrderListNumber")]
        public ApiResult<List<GetWorkOrderNumberListDto>> GetWorkOrderListNumber()
        {
            try
            {
                _logger.LogInformation("获取工单总数量");
                return _workOrderService.GetWorkOrderListNumber();
            }
            catch (Exception ex)
            {
                _logger.LogError("ex=>", ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 定时任务获取数据
        /// </summary>
        /// <returns></returns>

        [HttpGet("HangfireText")]

        public Standard.ApiResult<HAngfireDto> RefreshAllDataAsync([FromQuery]HangfirePageInput input)
        {
            try
            {
                _logger.LogInformation("Hangfire数据刷新成功");
                var resu=  _hangfireService.RefreshAllDataAsync(input);
                Console.WriteLine(resu.ResultData);
                return resu;
                
            }
            catch (Exception)
            {

                throw;
            }
        }
       
    }
}
